# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""Game base infrastructure"""
from __future__ import division, absolute_import
import pyglet
from src import conf, savegame

"""Game class contains anything related to game (game components/objects/container)
except additional stand-alone things such as text-messages, particles, and other"""

class DummyController(pyglet.event.EventDispatcher):
    def __init__(self):
        super(DummyController, self).__init__()
DummyController.register_event_type('on_building_create') 

# If we don't have real game controller created,
# so we don't have some game infrastructure created,
# we use dummy controller as a fuse for game.ctl calls
# todo: remove this shit
#ctl = DummyController()

def init(_win=None):
    global win, hud, vp, field
    global ctl, entityctl, unitctl, buildingctl

    win = _win

    from src import field, viewport
    field = field.Field(20, 20)
    vp = viewport.ViewPort(conf.vars['gfx.res_x'], conf.vars['gfx.res_y'])

    from src import hud
    hud = hud.HUD()
    win.push_handlers(hud)

    from src.gamectl import GameController
    ctl = GameController()

    # entities
    from src import entity
    entityctl = entity.EntityController()

    # units
    from src.unit import BaseUnit
    BaseUnit._ctl.push_handlers(ctl)
    unitctl = BaseUnit._ctl
    ctl.push_handlers(unitctl)
    win.push_handlers(unitctl) # fixme
    
    # buildings
    from src.building import Building
    Building._ctl.push_handlers(ctl)
    buildingctl = Building._ctl # FIXME change to Building.get_controller()
    ctl.push_handlers(buildingctl)

    win.register_event_type('on_update')
    def update(dt):
        win.dispatch_event('on_update', dt)
    pyglet.clock.schedule(update)

    # gui
    #global gui
    #from src.gui import GUI
    #gui = GUI(win=win)


def create():
    from src.units.tanks.lt1 import LT1
    u1 = LT1(4, 4)
    u2 = LT1(5, 4)
    u1.move_to(3, 3)
    from src.buildings.bank import Bank
    b1 = Bank(6, 6)
    b2 = Bank(10, 6)
    b2.take_damage(60)

load = savegame.load
save = savegame.save

def update(dt):
    unitctl._update(dt)
    hud.update()

def draw():
    win.clear()
    field.draw()
    hud.draw()
    for u in unitctl.get_all_units():
        u.draw()
    for b in buildingctl.get_all_buildings():
        b.view.draw()
    #gui.batch.draw()

